//
//  SegmentViewController.swift
//  shaohuo_reconsitution
//
//  Created by mac on 2019/1/12.
//  Copyright © 2019 mac. All rights reserved.
//

import UIKit

///iPhoneX适配
let IS_IPhoneX_All = UIScreen.main.bounds.height >= 812.0 ? true : false

let kNavHeight : CGFloat = IS_IPhoneX_All ? 88.0 : 64.0

let kTabBarHeight : CGFloat = IS_IPhoneX_All ? 34.0 : 0.0

let cellid = "cellid"

class SegmentViewController: UIViewController {
    
    static let share = SegmentViewController()
    
    var mainConllectionView : UICollectionView?
    
    var buttonArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
    
    }
    
    func addChildView(viewControllers:[UIViewController]) -> SegmentViewController {
        
        let buttonW = view.frame.width / 6
        
        let titleView = UIScrollView(frame: CGRect(x: 0, y: kNavHeight, width: view.bounds.width, height: 50))
        titleView.backgroundColor = UIColor.darkGray
        view.addSubview(titleView)
        
        let collectionH = view.frame.height - titleView.frame.maxY
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: view.frame.width, height: collectionH)
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: titleView.frame.maxY, width: view.frame.width, height: collectionH), collectionViewLayout: layout)
        collectionView.isPagingEnabled = true
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: cellid)
        collectionView.backgroundColor = UIColor.lightGray
        mainConllectionView = collectionView
        view.addSubview(collectionView)
        
        for (index,vc) in viewControllers.enumerated() {
            let titleButton = UIButton(type: .custom)
            titleButton.tag = 1000+index
            titleButton.frame = CGRect(x: buttonW * CGFloat(index), y: 0, width: buttonW, height: titleView.frame.height)
            titleButton.setTitle(vc.title, for: .normal)
            titleButton.setTitleColor(UIColor.gray, for: .normal)
            titleButton.setTitleColor(UIColor.yellow, for: .selected)
            titleButton.addTarget(self, action: #selector(titleBtnAction), for: .touchUpInside)
            buttonArray.add(titleButton)
            titleView.addSubview(titleButton)

        }
        
        return self
        
    }
    
    @objc func titleBtnAction(sender:UIButton) {
        for button in buttonArray as! [UIButton] {
            button.isSelected = false
        }
        sender.isSelected = true
        
        let index = sender.tag - 1000
        mainConllectionView?.setContentOffset(CGPoint(x: view.frame.width * CGFloat(index), y: 0), animated: true)
        
    }

}

extension SegmentViewController : UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return buttonArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath)
        
        if (indexPath.row % 2) != 0 {
            cell.backgroundColor = UIColor.blue
        }else {
            cell.backgroundColor = UIColor.red
        }

        return cell
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
//        if scrollView == mainConllectionView {
//            let i = Int(scrollView.contentOffset.x / view.frame.width)
//            titleBtnAction(sender: buttonArray[i] as! UIButton)
//        }
        
    }
    
}
